<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Image extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('image/image_model');
    }

    /**
     *
     * @param type $options['image_path'] 
     * @return $image_id 
     */
    public function upload_image($options = array())
    {
        $config['upload_path'] = UPLOAD_PATH;
        $config['allowed_types'] = ALLOWED_TYPES;
        $config['max_size'] = MAX_SIZE;
        $config['encrypt_name'] = ENCRYPT_NAME;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload() )
            return $this->upload->display_errors();
        else
        {

            $this->load->library('image_lib');
            
            $image = $this->upload->data();

            // Start processing the uploaded image
            $config['image_library'] = IMAGE_LIBRARY;
            $config['source_image'] = UPLOAD_PATH . $image['file_name'];
            $config['maintain_ratio'] = MAINTAIN_RATIO;

            // Create the MAIN image
            $width = isset ($options['width']) ? $options['width'] : MAIN_WIDTH;
            $height = isset ($options['height']) ? $options['height'] : MAIN_HEITH;
            
            $config['width'] = $width;
            $config['height'] = $height;
            if ($image['image_width'] < $image['image_height'])
            {
                $config['width'] = $height;
                $config['height'] = $width;
            }
            //TODO: Create directory if not existed ?????
            if(!file_exists(UPLOAD_PATH . $options['folder_name']))
                mkdir (UPLOAD_PATH . $options['folder_name']);
            
            $new_image_name = random_string('unique', date('YmdHis')) . $image['file_ext'];
            $config['new_image'] = UPLOAD_PATH . $options['folder_name'] . '/' . $new_image_name;

            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
            
            //Create thumbnail image
            if(isset($options['thumbnail'])){
                
                $width_thumb = isset($options['width_thumb']) ? $options['width_thumb'] : THUMBNAIL_WIDTH;
                $height_thumb = isset($options['height_thumb']) ? $options['height_thumb'] : THUMBNAIL_HEITH;

                $config['width'] = $width_thumb;
                $config['height'] = $height_thumb;
                
                if ($image['image_width'] < $image['image_height']) {
                    
                    $config['width'] = $height;
                    $config['height'] = $width;
                }
                
                if (!file_exists(UPLOAD_PATH . $options['folder_name'].'/thumbnail'))
                    mkdir(UPLOAD_PATH . $options['folder_name'].'/thumbnail');
                
                $config['new_image'] = UPLOAD_PATH . $options['folder_name'] . '/thumbnail/' . $new_image_name;
                
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
            }

            // physically delete image file
            if (file_exists(UPLOAD_PATH . $image['file_name']))
                unlink(UPLOAD_PATH . $image['file_name']);

            //save to DB and return PhotoID
            return $this->image_model->add_image(array('img_name' => $new_image_name));
            
        }
    }
    
    /**
     *
     * @param type $options['img_id'], $options['folder_name']
     * @return NULL if not found which needs to be deleted 
     */
    public function delete_image($options = array())
    {
        // Get image
        $photos = $this->image_model->get_image(array('id' => $options['img_id']));        
        if (count($photos) > 0)
        {
            // delete data from DB
            $this->image_model->delete_image(array('id' => $options['img_id'])); 

            // physically delete image file
            if (file_exists(UPLOAD_PATH . $options['folder_name'] . '/' . $photos[0]->img_name))
                unlink(UPLOAD_PATH . $options['folder_name'] . '/' . $photos[0]->img_name);
            if (file_exists(UPLOAD_PATH . $options['folder_name'] . '/thumbnail/' . $photos[0]->img_name))
                unlink(UPLOAD_PATH . $options['folder_name'] . '/thumbnail/' . $photos[0]->img_name);
        }
        return NULL;
    }
    

}
